package cc.monggo.utils.encode.impl;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cc.monggo.utils.encode.EncodeService;

public class EncodeServiceMD5Impl implements EncodeService {
	
	private static Log logger = LogFactory.getLog(EncodeServiceMD5Impl.class);
	
	@Override
	public String decode2Str(String input) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int decode2Int(String input) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long decode2Long(String input) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String encode(String input) {
		String result = "";
		try {
			result = md5Encode(input.getBytes("utf8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public String encode(int input){
		return null;
	}

	@Override
	public String encode(long input) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private String md5Encode(byte[] input){
		try{
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(input);
			byte[] result = md5.digest();
			StringBuilder ret=new StringBuilder(result.length<<1);
			for(int i=0;i<result.length;i++){
				ret.append(Character.forDigit((result[i]>>4)&0xf,16));
				ret.append(Character.forDigit(result[i]&0xf,16));
			}
			return ret.toString();
		}catch(NoSuchAlgorithmException e){
			logger.error("没有加密算法..........:" + e.getMessage());
		}
		return null;
	}

}
